var jsp = require('uglify-js').parser;
var pro = require('uglify-js').uglify;
var fs = require('fs');
var less = require('less');

var baseDir = '/Users/tiny_moo/Workspace/100kymsm/svn_webapp/assets';
var srcDir = baseDir + '/src';
var buildDir = baseDir + '/build/20130304';


var fileNameArr = ['meili'];

// 编译web_game_fp.less

fileNameArr.forEach(function (fileName) {
    console.log('input-fileName: ' + srcDir + '/' + fileName + '.less');

    fs.exists(srcDir + '/' + fileName + '.less', function (exists) {
        if (exists) {
            fs.readFile(srcDir + '/' + fileName + '.less', 'utf-8', function (err, data) {
                var parser;

                if (err) throw err;

                parser = new(less.Parser)({
                    paths: ['./src']
                });
                parser.parse(data, function (err, tree) {
                    /*
                    fs.writeFile(buildDir + '/' + fileName + '.css', tree.toCSS(), function (err) {
                        if (err) throw err;

                        console.log('output-fileName: ' + buildDir + '/' + fileName + '.css');
                    });
                    */
                    fs.writeFile(buildDir + '/' + fileName + '-min.css', tree.toCSS({
                        compress: true
                    }), function (err) {
                        if (err) throw err;

                        console.log('output-fileName: ' + buildDir + '/' + fileName + '-min.css');
                    });
                });
            });
        } else {
            console.error('cannot find the file');
        }
    });
});
/*
fs.exists(srcDir + '/home.js', function (exists) {
    if (exists) {
        fs.readFile(srcDir + '/home.js', 'utf-8', function (err, data) {
            var ast, code1, code2;

            if (err) throw err;

            console.log('get the content of this file');

            console.log('get the original ast');
            ast = jsp.parse(data);

            console.log('ascii the combined code');
            code1 = pro.gen_code(ast, {
                ascii_only: true,
                beautify: true
            });


            ast = pro.ast_mangle(ast);
            ast = pro.ast_squeeze(ast);

            console.log('get the compression code');
            code2 = pro.gen_code(ast, {
                ascii_only: true
            });
            fs.writeFile(buildDir + '/home-min.js', code2, function (err) {
                if (err) throw err;

                console.log('get compressed file');
            });
        });
    } else {
        console.error('cannot find the file');
    }
});
    */
fs.exists(srcDir + '/meili.js', function (exists) {
    if (exists) {
        fs.readFile(srcDir + '/meili.js', 'utf-8', function (err, data) {
            var ast, code1, code2;

            if (err) throw err;

            console.log('get the content of this file');

            console.log('get the original ast');
            ast = jsp.parse(data);

            console.log('ascii the combined code');
            code1 = pro.gen_code(ast, {
                ascii_only: true,
                beautify: true
            });


            ast = pro.ast_mangle(ast);
            ast = pro.ast_squeeze(ast);

            console.log('get the compression code');
            code2 = pro.gen_code(ast, {
                ascii_only: true
            });
            fs.writeFile(buildDir + '/meili-min.js', code2, function (err) {
                if (err) throw err;

                console.log('get compressed file');
            });
        });
    } else {
        console.error('cannot find the file');
    }
});